﻿<%@ WebHandler Language="C#" Class="FileCS" %>

  
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System;
using System.IO;


public class FileCS : IHttpHandler
{
   
    //// FLV header
    private static readonly byte[] _flvheader = HexToByte("464C5601010000000900000009");



    public void ProcessRequest(HttpContext context)
    {
        try
        {
            int pos;
            int length;

            // Check start parameter if present
            string image = (context.Request.QueryString["Video_Name"]);
            string filename = Path.GetFileName(context.Request.FilePath);
            //string filen = Server.MapPath("~/ConvertVideo/" + image);

            using (System.IO.FileStream fs = new System.IO.FileStream(context.Server.MapPath(filename), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
            {
                string qs = context.Request.Params["start"];

                if (string.IsNullOrEmpty(qs))
                {
                    pos = 0;
                    length = Convert.ToInt32(fs.Length);
                }
                else
                {
                    pos = Convert.ToInt32(qs);
                    length = Convert.ToInt32(fs.Length - pos) + _flvheader.Length;
                }

                // Add HTTP header stuff: cache, content type and length        
                context.Response.Cache.SetCacheability(HttpCacheability.Public);
                context.Response.Cache.SetLastModified(DateTime.Now);

                context.Response.AppendHeader("Content-Type", "video/x-flv");
                context.Response.AppendHeader("Content-Length", length.ToString());

                // Append FLV header when sending partial file
                if (pos > 0)
                {
                    context.Response.OutputStream.Write(_flvheader, 0, _flvheader.Length);
                    fs.Position = pos;
                }

                // Read buffer and write stream to the response stream
                const int buffersize = 16384;
                byte[] buffer = new byte[buffersize];

                int count = fs.Read(buffer, 0, buffersize);
                while (count > 0)
                {
                    if (context.Response.IsClientConnected)
                    {
                        context.Response.OutputStream.Write(buffer, 0, count);
                        context.Response.Flush();

                        count = fs.Read(buffer, 0, buffersize);
                    }
                    else
                    {
                        count = -1;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }

    public bool IsReusable
    {
        get { return true; }
    }

    private static byte[] HexToByte(string hexString)
    {
        byte[] returnBytes = new byte[hexString.Length / 2];
        for (int i = 0; i < returnBytes.Length; i++)
            returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
        return returnBytes;
    }
    //public void ProcessRequest(HttpContext context)
    //{
    //    int id = int.Parse(context.Request.QueryString["id"]);
    //    byte[] bytes;
    //    string contentType;
    //    string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    //    string name;
    //    using (SqlConnection con = new SqlConnection(strConnString))
    //    {
    //        using (SqlCommand cmd = new SqlCommand())
    //        {
    //            cmd.CommandText = "select Name, Data, ContentType from tbl_Video where Id=@Id";
    //            cmd.Parameters.AddWithValue("@Id", id);
    //            cmd.Connection = con;
    //            con.Open();
    //            SqlDataReader sdr = cmd.ExecuteReader();
    //            sdr.Read();
    //            bytes = (byte[])sdr["Data"];
    //            contentType = sdr["ContentType"].ToString();
    //            name = sdr["Name"].ToString();
    //            con.Close();
    //        }
    //    }
    //    context.Response.Clear();
    //    context.Response.Buffer = true;
    //    context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name);
    //     context.Response.ContentType = contentType;
    //    context.Response.BinaryWrite(bytes);
    //    context.Response.End();

    //    //context.Response.ContentType = reader["Name"].ToString();
    //    //context.Response.BinaryWrite((byte[])reader["Video"]);
    //}

    //public bool IsReusable
    //{
    //    get
    //    {
    //        return false;
    //    }
    //}
}
